/*
 * @Author: your name
 * @Date: 2021-05-30 11:17:58
 * @LastEditTime: 2022-01-16 20:36:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \telescope720P\src\page_handle.h
 */
#ifndef PAGE_HANDLE_H
#define PAGE_HANDLE_H
#include "gui_engine.h"
#include "xlog.h"
#include "common.h"
#include <memory>
#include <list>
#include <mutex>
enum class PageCmd
{
    REFRESH_SPEAKING_TIME = 0,
    PAGE_CMD_END
};
class KeyCmdMapTable
{
public:
    KeyCmd table[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_END)];
    KeyCmdMapTable()
    {
        for (int i = 0; i < static_cast<int>(KeyboardValue::KEYBOARD_VALUE_END); i++)
        {
            /**
             * @brief
             * 初始化所有键值的控制命令为默认（无操作）。
             */
            table[i] = KeyCmd::KEY_CMD_DEFAULT;
        }
        table[static_cast<int>(KeyboardValue::KEY_OFF_HOOK)] = KeyCmd::KEY_OFF_HOOK;
        table[static_cast<int>(KeyboardValue::KEY_ON_HOOK)] = KeyCmd::KEY_ON_HOOK;
        table[static_cast<int>(KeyboardValue::KEY_HANDS_FREE)] = KeyCmd::KEY_HANDS_FREE;
    }
};
class PageHandle
{
public:
    PageHandle();
    virtual ~PageHandle();
    bool PageOpen(std::string windowName);
    bool PageClose();
    void SetKeyEvent(KeyCmdMapTable event);
    void SaveContext(Context *ctx);
    Widget *GetThisPage();
    KeyCmdMapTable &GetKeyEvent();
    virtual void Select(void *ctx) {}
    virtual void KeyDownEventOK(void *ctx) {}
    virtual void ContrlSecondPage(void *ctx) {}
    virtual void UnlockSecondPage(void *ctx) {}
    /**
     * @brief
     * 密码界面确认抽象方法，当派生类打开了密码界面时使用。
     * @param ctx 上下文，传递路线：派生类界面->密码界面->派生类界面。
     */
    // virtual void PasswordPageConfirm(std::string password, void *ctx) {}
    /**
     * @brief
     * 界面统一的唯一外部消息入口
     */
    virtual void HandleInputMsg(HandleMessageToPage msg) {}
    void KeyEventRedefine(KeyboardValue key, KeyCmd cmd);
    void Release();
    /**
     * @brief
     * 当页面数据发生修改或者其它变化时，刷新显示的数据，例如：二级页数据刷新时
     */
    virtual void RefreshPage() {}
    std::unique_ptr<Widget> &GetUniquePtrPage();
    /**
     * @brief
     * 抽象的给界面发送命令的函数
     * @param cmd 命令
     */
    virtual void SendMsgToPage(PageCmd cmd) {}

private:
    void SetPage(Widget *p);
    /**
     * @brief
     * 界面的信息，本界面的控件指针
     */
    std::unique_ptr<Widget> page;
    KeyCmdMapTable keyEvent; //按键事件映射表
    std::list<Context *> ctxQueue;
    std::mutex m_mutex;
};
#endif